package com.corewillsoft.fireflies.ui;

import org.anddev.andengine.entity.Entity;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.font.FontFactory;
import org.anddev.andengine.opengl.texture.ITexture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.util.HorizontalAlign;

import android.content.Intent;
import android.graphics.Color;

import com.corewillsoft.fireflies.R;
import com.corewillsoft.fireflies.intent.IntentExtras;
import com.corewillsoft.fireflies.sounds.SoundsManager;
import com.corewillsoft.fireflies.storage.DbManager;
import com.corewillsoft.fireflies.ui.MapActivity.MapLevelType;
import com.corewillsoft.fireflies.util.ScreenUtils;

public abstract class BaseMapActivity extends BaseActivity implements IOnMenuItemClickListener {

	public final int BOTTOM_FONT_SIZE = 28;
	public final int BOTTOM_FONT_MARGIN = 16;
	public final int CONTROL_BAR_AREA_HEIGHT = BOTTOM_FONT_SIZE + BOTTOM_FONT_MARGIN;

	private Text backText;
	private Text continueText;
	private Rectangle continueContainer;
	private ITexture fontTexture;
	private Font font;

	protected final DbManager dbManager = new DbManager();

	@Override
	public void onLoadResources() {
		super.onLoadResources();
		fontTexture = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		font = FontFactory.createFromAsset(this.fontTexture, this, "Plok.ttf", BOTTOM_FONT_SIZE, true, Color.WHITE);
		getEngine().getTextureManager().loadTexture(this.fontTexture);
		getEngine().getFontManager().loadFont(this.font);
	}

	protected void createControlBar() {
		int yPosition = (int) (ScreenUtils.getInstance().getRelativeHeight() - BOTTOM_FONT_MARGIN - BOTTOM_FONT_SIZE);
		
		Rectangle backContainer = new Rectangle(0, yPosition, 0, CONTROL_BAR_AREA_HEIGHT) {

			@Override
			public boolean onAreaTouched(TouchEvent pTouchEvent, float pX, float pY) {
				if (pTouchEvent.getAction() == TouchEvent.ACTION_UP) {
					if (dbManager.hasSavedIncompletedLevels()) {
						finish();
					}
				}
				performClick(backText, pTouchEvent);
				return true;
			}
		};
		backContainer.setAlpha(0);
		scene.getChild(MapLevelType.NAVIGATION.ordinal()).attachChild(backContainer);
		scene.registerTouchArea(backContainer);

		backText = new Text(BOTTOM_FONT_MARGIN, 0, this.font, getString(R.string.back_to_menu), HorizontalAlign.LEFT);
		backText.setColor(0f, 0f, 0f);

		float backTextWidth = backText.getBaseWidth() + BOTTOM_FONT_MARGIN;
		backContainer.setWidth(backTextWidth);
		backContainer.attachChild(backText);

		// CONTINUE Button implementation
		continueContainer = new Rectangle(0, 0, 0, CONTROL_BAR_AREA_HEIGHT) {
			
			@Override
			public boolean onAreaTouched(TouchEvent pTouchEvent, float pX, float pY) {
				if (pTouchEvent.getAction() == TouchEvent.ACTION_UP) {
					if (dbManager.hasSavedIncompletedLevels()) {
						startGame(dbManager.getLastIncompletedLevelId(), false);
					}
				}
				performClick(continueText, pTouchEvent);
				return true;
			}
		};
		
		continueContainer.setAlpha(0);
		scene.getChild(MapLevelType.NAVIGATION.ordinal()).attachChild(continueContainer);
		scene.registerTouchArea(continueContainer);

		continueText = new Text(0, 0, this.font, getString(R.string.CONTINUE), HorizontalAlign.LEFT);
		continueText.setColor(0f, 0f, 0f);
		
		float continueTextWidth = continueText.getBaseWidth() + BOTTOM_FONT_MARGIN;
		continueContainer.setWidth(continueTextWidth);
		
		float xContinuePosition = ScreenUtils.getInstance().getRelativeWidth() - continueTextWidth;
		continueContainer.setPosition(xContinuePosition, yPosition);
		continueContainer.attachChild(continueText);

		updateContinueButton();
	}

	protected void updateContinueButton() {
		if ((continueText != null)) {
			if (dbManager.hasSavedIncompletedLevels()) {
				continueText.setAlpha(1);
				scene.registerTouchArea(continueContainer);
			} else {
				continueText.setAlpha(1);
				scene.unregisterTouchArea(continueContainer);
			}
		}
	}

	protected void startGame(int level, boolean newGame) {
		Intent intent = new Intent(this, GameActivity.class);
		intent.putExtra(IntentExtras.LEVEL_EXTRA, level);
		intent.putExtra(IntentExtras.NEW_GAME, newGame);
		startActivity(intent);
	}

	private void performClick(Entity entity, TouchEvent pTouchEvent) {
		switch (pTouchEvent.getAction()) {
		case TouchEvent.ACTION_DOWN:
			SoundsManager.getInstance().playMenuClick();
			entity.setColor(235, 0, 0);
			break;
		case TouchEvent.ACTION_UP:
			entity.setColor(0, 0, 0);
			break;
		default:
			break;
		}
	}

	@Override
	public void onLoadComplete() {
	}
}
